#!/usr/bin/env python
#coding: utf-8

import MySQLdb
import threading
import Queue

db_config={
        'user':'root',
        'passwd':'',
        'db':'test',
        'host':'localhost',
        }

def process(data_tuple):
    for data in data_tuple:
        print data

def get_data(row_count=500):
    cursor.execute('select count(*) from test')
    total=cursor.fetchone()[0]
    offset=0
    while 1:
        cursor.execute('select * from test limit %s,%s',(offset,row_count))
        yield cursor.fetchall()
        offset+=row_count
        if offset>=total:
            break
class Worker(threading.Thread):
    def run(self):
        while 1:
            args=queue.get()
            if args is None:
                break
            process(args)
            queue.task_done()


# --------------------------------------------------------------------
if __name__=='__main__':

    import time, sys
    if sys.platform == "win32":
            timer = time.clock
    else:
            timer = time.time

    t0, t1 = timer(), time.clock()
    try:
        count = int(sys.argv[1])
    except:
        count = 4
    conn=MySQLdb.connect(**db_config)
    cursor=conn.cursor()
    queue = Queue.Queue()
    for i in range(count):
        w=Worker()
        w.setDaemon(1)
        w.start()
    
    for data_tuple in get_data():
        queue.put(data_tuple)

    queue.join()
    print timer() - t0, time.clock() - t1
